home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Champak 52
/
Volume 52 - JOGO DISK .iso
/
Games
/
skipandgouls.swf
/
scripts
/
__Packages
/
maze
/
MazeGenerator.as
< prev
next >
Wrap
Text File
|
2007-10-01
|
15KB
|
452 lines
class maze.MazeGenerator
{
function MazeGenerator(l_oModel)
{
this.oModel = l_oModel;
this.aGrid = this.oModel.aGrid;
this.nX = 0;
this.nY = 0;
this.oRandom = new sarbakan.utils.PredefinedRandom();
}
function generate(l_bBonusLevel)
{
var _loc4_ = undefined;
this.generateBorders();
this.nX = 0;
this.nY = 4;
while(this.nY < this.oModel.nHeight - (maze.MazeData.FLOORS_MAX_HEIGHT + 2))
{
this.nX = 0;
while(this.nX < this.oModel.nWidth)
{
this.generateAtCurrentPos();
this.nX = this.nX + 1;
}
this.nY = this.nY + 1;
}
var _loc2_ = this.getWalkWays();
this.generatePipes(_loc2_,l_bBonusLevel);
this.generateEndFloor();
if(!l_bBonusLevel)
{
_loc4_ = this.generateEnemies(_loc2_);
}
this.generateBonuses(_loc2_,l_bBonusLevel);
this.generateDecoration(_loc2_,_loc4_);
}
function generateAtCurrentPos()
{
if(this.underFloor() && this.canCreateWall())
{
this.createWall();
}
else if((this.underWalkWay() || this.underHole()) && this.canCreateFloor())
{
this.createFloor();
}
}
function generatePipes(l_aWalkWays, l_bBonusLevel)
{
for(var _loc7_ in l_aWalkWays)
{
var _loc4_ = 0;
var _loc5_ = l_aWalkWays[_loc7_].width / 3;
if(this.isWalkWayIsolated(l_aWalkWays[_loc7_].x,l_aWalkWays[_loc7_].y,l_aWalkWays[_loc7_].width))
{
this.createPipe(l_aWalkWays[_loc7_]);
_loc4_ = _loc4_ + 1;
}
if(l_bBonusLevel)
{
var _loc3_ = 0;
while(_loc3_ < l_aWalkWays[_loc7_].width)
{
if(_loc4_ >= _loc5_)
{
break;
}
if(Math.random() < maze.MazeData.CHANCE_PIPE_CREATION)
{
if(this.createPipe(l_aWalkWays[_loc7_]))
{
_loc4_ = _loc4_ + 1;
}
}
_loc3_ = _loc3_ + 1;
}
}
}
}
function generateBorders()
{
this.nY = 0;
while(this.nY < this.oModel.nHeight)
{
this.nX = 0;
this.createWall();
this.nX = this.oModel.nWidth - 1;
this.createWall();
this.nY = this.nY + 1;
}
}
function generateEndFloor()
{
this.nY = this.oModel.nHeight - 1;
this.nX = 0;
while(this.nX < this.oModel.nWidth)
{
this.createWall();
this.nX = this.nX + 1;
}
this.createPipe({x:1,y:this.nY - 1,width:this.oModel.nWidth - 2},true);
this.createExit(1 + Math.floor(this.oRandom.getLastGetted() * (this.oModel.nWidth - 2)),this.nY - 2);
}
function createExit(l_nX, l_nY)
{
this.oModel.addElementExit(l_nX,l_nY);
}
function generateEnemies(l_aWalkWays)
{
var _loc5_ = false;
var _loc6_ = undefined;
for(var _loc7_ in l_aWalkWays)
{
if(this.canCreateEnemy(l_aWalkWays[_loc7_]))
{
var _loc4_ = l_aWalkWays[_loc7_].x + Math.floor(Math.random() * l_aWalkWays[_loc7_].width);
_loc6_ = this.aGrid[l_aWalkWays[_loc7_].y - 1][_loc4_] != maze.MazeData.TILE_EMPTY;
var _loc3_ = Math.random();
if(_loc3_ <= maze.MazeData.CHANCE_DUTCHMAN_CREATION)
{
this.createDutchman(l_aWalkWays[_loc7_].x + sarbakan.utils.MoreMath.random(1,l_aWalkWays[_loc7_].width - 2),l_aWalkWays[_loc7_].y);
_loc5_ = true;
}
else if(_loc3_ <= maze.MazeData.CHANCE_SKELETUNA_CREATION)
{
this.createSkeletuna(l_aWalkWays[_loc7_].x,l_aWalkWays[_loc7_].y);
}
else if(_loc3_ <= maze.MazeData.CHANCE_VAMPIRAY_CREATION && _loc6_)
{
this.createVampiray(_loc4_,l_aWalkWays[_loc7_].y);
}
else if(Math.random() < 0.6)
{
this.createDutchman(l_aWalkWays[_loc7_].x + sarbakan.utils.MoreMath.random(1,l_aWalkWays[_loc7_].width - 2),l_aWalkWays[_loc7_].y);
_loc5_ = true;
}
else
{
this.createSkeletuna(l_aWalkWays[_loc7_].x,l_aWalkWays[_loc7_].y);
}
}
}
return _loc5_;
}
function canCreateEnemy(l_aWalkWay)
{
return l_aWalkWay.width > 2 && Math.random() < maze.MazeData.CHANCE_ENEMY_CREATION;
}
function createVampiray(l_nX, l_nY)
{
this.oModel.addElementVampiRay(l_nX,l_nY);
}
function createDutchman(l_nX, l_nY)
{
this.oModel.addElementDutchMan(l_nX,l_nY);
}
function createSkeletuna(l_nX, l_nY, l_nWidth)
{
this.oModel.addElementSkeletuna(l_nX + 2,l_nY);
}
function generateBonuses(l_aWalkWays, l_bBonusLevel)
{
var _loc6_ = undefined;
if(_global.C.oUnlockedCodes.data.RANDOM_WAFFLES)
{
_loc6_ = maze.MazeData.CHANCE_BONUS_CREATION_WAFFLECODE;
}
else
{
_loc6_ = maze.MazeData.CHANCE_BONUS_CREATION;
}
for(var _loc8_ in l_aWalkWays)
{
if(l_bBonusLevel)
{
var _loc4_ = l_aWalkWays[_loc8_].x;
while(_loc4_ < l_aWalkWays[_loc8_].x + l_aWalkWays[_loc8_].width)
{
this.createBurger(_loc4_,l_aWalkWays[_loc8_].y,true);
_loc4_ = _loc4_ + 1;
}
}
else if(Math.random() < _loc6_)
{
var _loc5_ = Math.random();
_loc4_ = l_aWalkWays[_loc8_].x + Math.floor(Math.random() * l_aWalkWays[_loc8_].width);
if(_loc5_ <= maze.MazeData.CHANCE_TOKEN_CREATION)
{
this.createToken(_loc4_,l_aWalkWays[_loc8_].y);
}
else if(_loc5_ <= maze.MazeData.CHANCE_PUMPKIN_CREATION)
{
this.createPumpkin(_loc4_,l_aWalkWays[_loc8_].y);
}
else if(_loc5_ <= maze.MazeData.CHANCE_SLUG_CREATION)
{
this.createSlug(_loc4_,l_aWalkWays[_loc8_].y);
}
else if(_loc5_ <= maze.MazeData.CHANCE_BURGER_CREATION)
{
this.createBurger(_loc4_,l_aWalkWays[_loc8_].y);
}
}
}
}
function createToken(l_nX, l_nY)
{
this.oModel.addElementToken(l_nX,l_nY);
}
function createPumpkin(l_nX, l_nY)
{
this.oModel.addElementPumpkin(l_nX,l_nY);
}
function createSlug(l_nX, l_nY)
{
this.oModel.addElementSlug(l_nX,l_nY);
}
function createBurger(l_nX, l_nY, l_bBonusLevel)
{
if(_global.C.oUnlockedCodes.data.RANDOM_WAFFLES || l_bBonusLevel)
{
this.oModel.addElementWaffle(l_nX,l_nY);
}
else
{
this.oModel.addElementBurger(l_nX,l_nY);
}
}
function generateDecoration(l_aWalkWays, l_bDutchmanCreated)
{
for(var _loc5_ in l_aWalkWays)
{
if(this.canGenerateSpiderWeb(l_aWalkWays[_loc5_]))
{
this.createSpiderWeb(l_aWalkWays[_loc5_]);
}
if(this.canGenerateWindow(l_aWalkWays[_loc5_]) && !l_bDutchmanCreated)
{
var _loc3_ = 1;
if(l_aWalkWays[_loc5_].width >= 4)
{
_loc3_ = _loc3_ + 1;
}
if(l_aWalkWays[_loc5_].width >= 6)
{
_loc3_ = _loc3_ + 1;
}
this.createWindow(l_aWalkWays[_loc5_],_loc3_);
}
if(this.canGenerateCandle())
{
this.createCandle(l_aWalkWays[_loc5_]);
}
}
}
function underFloor()
{
return this.nY == 0 || this.aGrid[this.nY - 1][this.nX] == maze.MazeData.TILE_FLOOR;
}
function underWalkWay()
{
return this.nY == 1 || this.nY > 1 && this.aGrid[this.nY - 1][this.nX] == maze.MazeData.TILE_EMPTY && this.aGrid[this.nY - 2][this.nX] == maze.MazeData.TILE_FLOOR;
}
function underHole()
{
return this.nY > 1 && this.aGrid[this.nY - 1][this.nX] == maze.MazeData.TILE_EMPTY && this.aGrid[this.nY - 2][this.nX] == maze.MazeData.TILE_EMPTY;
}
function canCreateFloor()
{
return !this.mustCreateHole() && this.oModel.nWidth - this.nX >= maze.MazeData.FLOORS_MIN_WIDTH && this.isGridFree(maze.MazeData.FLOORS_MIN_WIDTH,maze.MazeData.FLOORS_MIN_HEIGHT) && Math.random() < maze.MazeData.CHANCE_FLOOR_CREATION;
}
function mustCreateHole()
{
return this.aGrid[this.nY][this.nX - 1] == maze.MazeData.TILE_EMPTY && this.aGrid[this.nY][this.nX - 2] != maze.MazeData.TILE_EMPTY;
}
function createFloor()
{
var _loc2_ = undefined;
_loc2_ = maze.MazeData.getRandomFloor();
while(_loc2_.width > this.oModel.nWidth - this.nX || !this.isGridFree(_loc2_.width,_loc2_.height))
{
_loc2_ = maze.MazeData.getRandomFloor();
}
this.fillGrid(maze.MazeData.TILE_FLOOR,_loc2_.width,_loc2_.height);
this.oModel.addElementFloor(this.nX,this.nY,_loc2_.width,_loc2_.height);
}
function canCreateWall()
{
return this.oModel.nWidth - this.nX >= 1 && this.aGrid[this.nY + 1][this.nX] == maze.MazeData.TILE_EMPTY && Math.random() < maze.MazeData.CHANCE_WALL_CREATION && this.isGridFree(1,1);
}
function createWall()
{
this.fillGrid(maze.MazeData.TILE_WALL,1,1);
this.oModel.addElementWall(this.nX,this.nY);
}
function createPipe(l_oWalkWay, l_bLastPipe)
{
var _loc4_ = l_oWalkWay.x + Math.floor(this.oRandom["get"]() * l_oWalkWay.width);
var _loc3_ = l_oWalkWay.y + 1;
var _loc5_ = 0;
if(this.aGrid[_loc3_][_loc4_] != maze.MazeData.TILE_PIPE)
{
this.aGrid[_loc3_][_loc4_] = maze.MazeData.TILE_PIPE;
var _loc2_ = _loc3_;
while(_loc2_ < this.oModel.nHeight)
{
if(this.aGrid[_loc2_][_loc4_] == maze.MazeData.TILE_EMPTY)
{
break;
}
_loc5_ = _loc5_ + 1;
_loc2_ = _loc2_ + 1;
}
this.oModel.addElementPipe(_loc4_,_loc3_,_loc5_,l_bLastPipe);
return true;
}
return this.createPipe(l_oWalkWay,l_bLastPipe);
}
function createSpiderWeb(l_aWalkWay)
{
var _loc5_ = undefined;
var _loc4_ = undefined;
var _loc3_ = undefined;
if(Math.random() < 0.5)
{
_loc4_ = l_aWalkWay.x;
_loc3_ = l_aWalkWay.y;
_loc5_ = true;
}
else
{
_loc4_ = l_aWalkWay.x + l_aWalkWay.width;
_loc3_ = l_aWalkWay.y;
_loc5_ = false;
}
this.oModel.addElementSpiderWeb(_loc4_,_loc3_,_loc5_);
}
function createWindow(l_aWalkWay, l_nNumber)
{
this.oModel.addElementWindow(l_aWalkWay.x,l_aWalkWay.y,l_aWalkWay.width,l_nNumber);
}
function createCandle(l_aWalkWay)
{
this.oModel.addElementCandle(l_aWalkWay.x,l_aWalkWay.y);
}
function canGenerateSpiderWeb(l_aWalkWay)
{
return this.isWalkWayIsolated(l_aWalkWay.x,l_aWalkWay.y,l_aWalkWay.width) && Math.random() < maze.MazeData.CHANCE_SPIDERWEB_CREATION;
}
function canGenerateWindow(l_aWalkWay)
{
return l_aWalkWay.width > 1 && Math.random() < maze.MazeData.CHANCE_WINDOW_CREATION;
}
function canGenerateCandle()
{
return Math.random() < maze.MazeData.CHANCE_CANDLE_CREATION;
}
function fillGrid(l_nTileType, l_nWidth, l_nHeight)
{
var _loc3_ = this.nY;
while(_loc3_ < this.nY + l_nHeight)
{
var _loc2_ = this.nX;
while(_loc2_ < this.nX + l_nWidth)
{
this.aGrid[_loc3_][_loc2_] = l_nTileType;
_loc2_ = _loc2_ + 1;
}
_loc3_ = _loc3_ + 1;
}
}
function isGridFree(l_nWidth, l_nHeight)
{
var _loc3_ = this.nY;
while(_loc3_ < this.nY + l_nHeight)
{
var _loc2_ = this.nX;
while(_loc2_ < this.nX + l_nWidth)
{
if(this.aGrid[_loc3_][_loc2_] != maze.MazeData.TILE_EMPTY)
{
return false;
}
_loc2_ = _loc2_ + 1;
}
_loc3_ = _loc3_ + 1;
}
return true;
}
function getWalkWays()
{
var _loc4_ = [];
var _loc2_ = false;
var _loc3_ = {};
var _loc7_ = 0;
while(_loc7_ < this.oModel.nHeight - (maze.MazeData.FLOORS_MAX_HEIGHT + 2))
{
var _loc5_ = 0;
while(_loc5_ < this.oModel.nWidth)
{
if(_loc2_)
{
if(this.aGrid[_loc7_][_loc5_] == maze.MazeData.TILE_EMPTY && this.aGrid[_loc7_ + 1][_loc5_] != maze.MazeData.TILE_EMPTY)
{
_loc3_.width = _loc3_.width + 1;
if(_loc5_ == this.oModel.nWidth - 1)
{
_loc4_.push(_loc3_);
_loc2_ = false;
}
}
else
{
_loc2_ = false;
_loc4_.push(_loc3_);
}
}
else if(this.aGrid[_loc7_][_loc5_] == maze.MazeData.TILE_EMPTY && this.aGrid[_loc7_ + 1][_loc5_] != maze.MazeData.TILE_EMPTY)
{
_loc2_ = true;
_loc3_ = {x:_loc5_,y:_loc7_,width:1};
if(_loc5_ == this.oModel.nWidth - 1)
{
_loc4_.push(_loc3_);
_loc2_ = false;
}
}
_loc5_ = _loc5_ + 1;
}
_loc7_ = _loc7_ + 1;
}
return _loc4_;
}
function isWalkWayIsolated(l_nX, l_nY, l_nWidth)
{
var _loc3_ = false;
var _loc5_ = false;
if(l_nX > 0 && this.aGrid[l_nY][l_nX - 1] == maze.MazeData.TILE_EMPTY && this.aGrid[l_nY + 1][l_nX - 1] == maze.MazeData.TILE_EMPTY)
{
_loc3_ = true;
}
if(l_nX < this.oModel.nWidth && this.aGrid[l_nY][l_nX + l_nWidth] == maze.MazeData.TILE_EMPTY && this.aGrid[l_nY + 1][l_nX + l_nWidth] == maze.MazeData.TILE_EMPTY)
{
_loc5_ = true;
}
if(!_loc3_ && !_loc5_)
{
return true;
}
}
}